package com.km.team.phsystem.web.controller;

import com.km.team.phsystem.beans.pojo.SystemCommonConf;
import com.km.team.phsystem.beans.vo.ResponseData;
import com.km.team.phsystem.beans.vo.SystemCommonConfVo;
import com.km.team.phsystem.common.constants.Constants;
import com.km.team.phsystem.common.enums.ResultEnum;
import com.km.team.phsystem.web.controller.base.BaseController;
import com.km.team.phsystem.web.utils.ResponseDataUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: rochzhu
 * Date: 2018/10/16
 * Time: 19:00pm
 * Description: 业务类型
 */
@RestController
@RequestMapping("/busitype")
public class BusiTypeController extends BaseController<BusiTypeController> {

    /**
     * 获取业务类型列表
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/queryBusiTypeData", produces = "application/json;charset=UTF-8")
    public ResponseData queryBusiTypeData() throws Exception {

       return ResponseDataUtil.buildSuccess(busiTypeService.getBusiTypeData());
    }

    @ApiOperation(value="新增业务类型", notes="")
    @PostMapping("/add")
    public ResponseData add(@RequestBody @Valid SystemCommonConfVo systemCommonConfVo, BindingResult bindingResult) throws Exception {

        if(bindingResult.hasErrors()){
            return ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        List<SystemCommonConf> list = busiTypeService.getBusiType(systemCommonConfVo);
        if (!CollectionUtils.isEmpty(list)){
            return ResponseDataUtil.buildError(Constants.ERROR, "业务类型名称重复！");
        }
        int insertflag = busiTypeService.add(systemCommonConfVo);
        if(insertflag>0){
            return ResponseDataUtil.buildSuccess();
        }else {
            return ResponseDataUtil.buildError(ResultEnum.BUSINESS_ERROR);
        }

    }

    /**
     * 编辑业务类型
     * @param busitype
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public ResponseData update(@RequestBody SystemCommonConf busitype) throws Exception {
        if(busitype == null){
            ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        busiTypeService.update(busitype);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 删除业务类型
     * @param busitype
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public ResponseData delete(@RequestBody SystemCommonConf busitype) throws Exception {
        if(busitype == null){
            ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        busiTypeService.delete(busitype);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 获取业务容量短信阈值
     * @return
     * @throws Exception
     */
    @GetMapping("/getSmsCapacity")
    public ResponseData getSmsCapacity() throws Exception {
        return ResponseDataUtil.buildSuccess(busiTypeService.getSmsCapacity());
    }

    /**
     * 修改业务容量短信阈值
     * @param busitype
     * @return
     * @throws Exception
     */
    @PostMapping("/updateSmsCapacity")
    public ResponseData updateSmsCapacity(@RequestBody SystemCommonConf busitype) throws Exception {
        if(busitype == null){
            ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        busiTypeService.update(busitype);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 查询短信发送截止时间
     * @return
     * @throws Exception
     */
    @GetMapping("/getSmsSendTime")
    public ResponseData getSmsSendTime() throws Exception {
        return ResponseDataUtil.buildSuccess(busiTypeService.getSmsSendTime());
    }

    /**
     * 修改短信发送截止时间
     * @param busitype
     * @return
     * @throws Exception
     */
    @PostMapping("/updateSmsSendTime")
    public ResponseData updateSmsSendTime(@RequestBody SystemCommonConf busitype) throws Exception {
        if(busitype == null){
            ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        busiTypeService.update(busitype);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 修改短信发送的相关设置
     * @param sms_capacity：业务容量短信阈值即排号数
     * @param sms_sendtime：每天短信发送截止时间
     * @param sms_arrivetime：发送短信后多长时间到大厅办理
     * @return
     * @throws Exception
     */
    @PostMapping("/updateSmsConfParam/{sms_capacity}/{sms_sendtime}/{sms_arrivetime}")
    public ResponseData updateSmsConfParam(@PathVariable("sms_capacity") String sms_capacity,
                                       @PathVariable("sms_sendtime") String sms_sendtime,
                                       @PathVariable("sms_arrivetime") String sms_arrivetime) throws Exception {
        List<SystemCommonConf> list_sms_capacity = busiTypeService.getSmsCapacity();
        List<SystemCommonConf> list_sms_sendtime = busiTypeService.getSmsSendTime();
        List<SystemCommonConf> list_sms_arrivetime = busiTypeService.getSmsArriveTime();
        int sms_capacity_id = list_sms_capacity.get(0).getId();
        int sms_sendtime_id = list_sms_sendtime.get(0).getId();
        int sms_arrivetime_id = list_sms_arrivetime.get(0).getId();
        SystemCommonConf sms_capacity_obj = new SystemCommonConf();
        sms_capacity_obj.setId(sms_capacity_id);
        sms_capacity_obj.setSysValue(sms_capacity);

        SystemCommonConf sms_sendtime_obj = new SystemCommonConf();
        sms_sendtime_obj.setId(sms_sendtime_id);
        sms_sendtime_obj.setSysValue(sms_sendtime);

        SystemCommonConf sms_arrivetime_obj = new SystemCommonConf();
        sms_arrivetime_obj.setId(sms_arrivetime_id);
        sms_arrivetime_obj.setSysValue(sms_arrivetime);

        busiTypeService.update(sms_capacity_obj);
        busiTypeService.update(sms_sendtime_obj);
        busiTypeService.update(sms_arrivetime_obj);
        return ResponseDataUtil.buildSuccess();
    }

    /**
     * 获取短信发送的相关设置
     * smscapacity：业务容量短信阈值即排号数
     * smssendtime：每天短信发送截止时间
     * smsarrivetime：发送短信后多长时间到大厅办理
     * @return
     * @throws Exception
     */
    @GetMapping("/getSmsConfParam")
    public ResponseData getSmsConfParam() throws Exception {
        List<SystemCommonConf> total = new ArrayList<SystemCommonConf>();
        List<SystemCommonConf> smscapacity = busiTypeService.getSmsCapacity();
        List<SystemCommonConf> smssendtime = busiTypeService.getSmsSendTime();
        List<SystemCommonConf> smsarrivetime = busiTypeService.getSmsArriveTime();
        if(smscapacity.size()>0){
            for( int i=0;i<smscapacity.size();i++){
                SystemCommonConf obj = smscapacity.get(i);
                total.add(obj);
            }
        }
        if(smssendtime.size()>0){
            for( int i=0;i<smssendtime.size();i++){
                SystemCommonConf obj = smssendtime.get(i);
                total.add(obj);
            }
        }
        if(smsarrivetime.size()>0){
            for( int i=0;i<smsarrivetime.size();i++){
                SystemCommonConf obj = smsarrivetime.get(i);
                total.add(obj);
            }
        }
        return ResponseDataUtil.buildSuccess(total);
    }
}
